package com.amms.android;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import com.amms.android.entities.Player;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DBHelper {
    

    private static final String DATABASE_CREATE =
        "create table player (id text primary key, "
            + "alias text not null,  age text not null, gender text not null);";

    private static final String DATABASE_NAME = "amms";

    private static final String DATABASE_TABLE = "player";

    private static final int DATABASE_VERSION = 1;

    private SQLiteDatabase db;

    public DBHelper(Context ctx) {
        try {
            db = ctx.openDatabase(DATABASE_NAME, null);
        } catch (FileNotFoundException e) {
            try {
                db =
                    ctx.createDatabase(DATABASE_NAME, DATABASE_VERSION, 0,
                        null);
                db.execSQL(DATABASE_CREATE);
            } catch (FileNotFoundException e1) {
                db = null;
            }
        }
    }

    public void close() {
        db.close();
    }

    public void createPlayer(String id,String alias, String age,String gender) {
        ContentValues initialValues = new ContentValues();
        initialValues.put("id", id);
        initialValues.put("alias", alias);
        initialValues.put("age", age);
        initialValues.put("gender", gender);
        db.insert(DATABASE_TABLE, null, initialValues);
    }

    public void deletePlayer(long id) {
        db.delete(DATABASE_TABLE, "id=" + id, null);
    }

    public List<Player> fetchAllPlayers() {
        ArrayList<Player> ret = new ArrayList<Player>();
        try {
            Cursor c =
                db.query(DATABASE_TABLE, new String[] {
                    "id", "alias", "age","gender"}, null, null, null, null, null);
            int numRows = c.count();
            c.first();
            for (int i = 0; i < numRows; ++i) {
            	Player player = new Player();
            	player.setId(c.getString(0));
            	player.setAlias(c.getString(1));
            	player.setAge(c.getString(2));
            	player.setGender(c.getString(3));
                ret.add(player);
                c.next();
            }
        } catch (SQLException e) {
            Log.e("booga", e.toString());
        }
        return ret;
    }

    public Player fetchPlayer(long id) {
    	Player player = new Player();
        Cursor c =
            db.query(true, DATABASE_TABLE, new String[] {
                "id", "alias", "age","gender"}, "id=" + id, null, null,
                null, null);
        if (c.count() > 0) {
            c.first();
            player.setId(c.getString(0));
        	player.setAlias(c.getString(1));
        	player.setAge(c.getString(2));
        	player.setGender(c.getString(3));
            return player;
        } else {
        	 player.setId("-1");
        	 player.setAlias(null);
         	player.setAge(null);
         	player.setGender(null);
        }
        return player;
    }

    public void updatePlayer(long id, String alias, String age,String gender) {
        ContentValues args = new ContentValues();
        args.put("alias", alias);
        args.put("age", age);
        args.put("gender", gender);
        db.update(DATABASE_TABLE, args, "id=" + id, null);
    }
}


